package com.avengers.core.bean.enums;

import java.util.Arrays;
import java.util.List;

/**
 * @author yu.wang
 * @since 12/29/22 4:57 PM
 **/
public enum RegistrarGroup {
    /**
     * 自有国内
     */
    ownCnnic(true,
            RegistrarCode.ynld,
            RegistrarCode.cnet,
            RegistrarCode.bjwz,
            RegistrarCode.tjzr,
            RegistrarCode.zyzk,
            RegistrarCode.yiso,
            RegistrarCode.zksf,
            RegistrarCode.bjzh,
            RegistrarCode.hqhy,
            RegistrarCode.huyi,
            RegistrarCode.youq,
            RegistrarCode.cert,
            RegistrarCode.brma,
            RegistrarCode.bjzy,
            RegistrarCode.nih,
            RegistrarCode.hmjd,
            RegistrarCode.xjny,
            RegistrarCode.ynhd,
            RegistrarCode.dfwj,
            RegistrarCode.wjpp
    ),
    /**
     * 自有国际
     */
    ownVerisign(true,
            RegistrarCode.dom1lim,
            RegistrarCode.dom2lim,
            RegistrarCode.dom3lim,
            RegistrarCode.dom4lim,
            RegistrarCode.hzbest,
            RegistrarCode.seventd,
            RegistrarCode.trebj
    ),
    /**
     * reg 龙名
     */
    regLongMing(false,
            RegistrarCode.cdns,
            RegistrarCode.doinse,
            RegistrarCode.qxwl,
            RegistrarCode.sjcl,
            RegistrarCode.sksat,
            RegistrarCode.weichuang,
            RegistrarCode.zheng
    ),
    /**
     * 其他所有
     * 2023-03-16 : thirdPlatform, _66, _22, _190, aliyun, bizCn, dropCatch, dynaDot, eName, juMing, nameCheap, naWang, snapNames, tencent, zzy
     */
    others(false);

    public final boolean isOwn;
    public final List<RegistrarCode> registrars;
    public final boolean hasRegistrars;

    RegistrarGroup(boolean isOwn, RegistrarCode... registrars) {
        this.isOwn = isOwn;
        this.hasRegistrars = null != registrars && registrars.length > 0;
        this.registrars = this.hasRegistrars ? Arrays.asList(registrars) : null;
    }

    public static RegistrarGroup of(String registrar) {
        if (null == registrar || registrar.isEmpty()) {
            return null;
        }
        for (RegistrarGroup group : values()) {
            if (null == group.registrars) {
                continue;
            }
            if (group.registrars.parallelStream().anyMatch(r -> r.name().equalsIgnoreCase(registrar))) {
                return group;
            }
        }
        return others;
    }

    public static RegistrarGroup of(RegistrarCode registrar) {
        if (null == registrar) {
            return null;
        }
        for (RegistrarGroup group : values()) {
            if (null == group.registrars) {
                continue;
            }
            if (group.registrars.contains(registrar)) {
                return group;
            }
        }
        return others;
    }
}
